<template>
  <a-modal :title="title" style="top: 100px;" :visible="true" width="1000px" @cancel="handleCancel">
    <div class="tips">
      当前期数：<span style="color:#1890ff;">{{session.session}}</span> （{{session.start_day}} ~ {{session.end_day}}）
        <a-button v-if="session.prev_session" type="primary" class="add" @click="initList(session.prev_session)" style="margin-right:10px;">
          上一周
        </a-button> 
        <a-button v-if="session.next_session" type="primary" class="add" @click="initList(session.next_session)">
          下一周
        </a-button>
    </div>
    <a-table class="product-table" :rowKey="row => row.id" :dataSource="list" bordered :pagination="false" size="small">
      <a-table-column title="名次" width="40px">
        <template slot-scope="text, record, index">
          <div>第{{ index+1 }}名</div>
        </template>
      </a-table-column>
      <a-table-column title="用户" width="120px">
        <template slot-scope="row">
          <div class="user">
            <img :src="row.user.headimg" class="thumb" alt="用户头像">
            <div>
              <div>{{row.user.name}}</div>
              <div>{{row.user.phone}}</div>
            </div>
          </div>
        </template>
      </a-table-column>
      <a-table-column title="本周佣金" key="number" width="100px">
        <template slot-scope="row">
          <div>
            <NumberGroupDisplay :info="row" prefix="total_"></NumberGroupDisplay>
          </div>
        </template>
      </a-table-column>
      <a-table-column v-if="reward.length > 0" title="获得奖励物品" width="120px">
        <template slot-scope="row">
            <div>{{row.reward_sku_title}}</div>
        </template>
      </a-table-column>      
      <a-table-column v-if="reward.length == 0" title="操作" width="40px">
        <template slot-scope="row">
          <a href="javascript:;" @click="handleDelete(row)">删除</a>
        </template>
      </a-table-column>
    </a-table>
    <template slot="footer">
      <div class="btn-c">
        <a-button v-if="reward.length === 0 && list.length > 0 && session.next_session" type="primary" class="add" @click="doReward" style="margin-left:20px">
          确认发放本周（{{session.start_day}} ~ {{session.end_day}}）排行榜奖励
        </a-button>
        <div style="flex-grow: 1;"></div>

        <a-button key="back" @click="handleCancel">关闭</a-button>
      </div>
    </template>
  </a-modal>
</template>
<script>
  import UploadImages from '@/components/UploadImages/UploadImages'
  import iSwitch from '@/components/ISwitch/ISwitch'
  import ISkuType from '@/components/ISkuType/ISkuType'
  import NumberGroupDisplay from '@/views/stats/components/NumberGroupDisplay'
  
  export default {
    components: {
      UploadImages,
      iSwitch,
      ISkuType,
      NumberGroupDisplay
    },
    props: {
      info: {
        type: Object,
        default () {
          return {}
        }
      }
    },
    data() {
      return {
        title: '',
        title2: '',
        session: {},
        reward: [],
        rankTypeList: [
          {
            type: 1,
            title: '用户周排名',
          },
          {
            type: 2,
            title: '用户月排名',
          }
        ],
        list: [],
      }
    },
    watch: {
      uuid() {
        this.initList()
      },
    },
    created() {
      this.initList()
      let item = this.rankTypeList;
      let now_type = this.rankTypeList.find(item => item.type === this.info.rank_type)
      this.title = '审核【' + now_type.title + '】'
      this.title2 = now_type.title
    },
    methods: {
      initList(session = '') {
        this.$http.get(`/agent/rank`, {
          params: {
            rank_type: this.info.rank_type,
            session: session
          }
        }).then(res => {
          this.list = res.data.list
          this.session = res.data.session
          this.reward = res.data.reward
        })
      },
      doReward() {
        this.$confirm({
          title: '提示',
          content: `确认发放本期（${this.session.start_day} ~ ${this.session.end_day}）奖励吗？`,
          onOk: () => {
            this.$http.post(`/agent/rank/doReward`, {
              rank_type: this.info.rank_type,
              session: this.session.start_day
            }).then(res => {
              this.$message.success('奖励发放成功')
              this.initList(this.session.start_day)
            })
          }
        })
      },      
      handleCancel() {
        this.$emit('refresh')
        this.$emit('close')
      },
      handleSave() {
        this.$emit('refresh')
        this.$emit('close')
        // this.$http.put(`/activities/${this.uuid}/skus/batch`, {
        //   list: this.list
        // }).then(res => {
        //   this.$message.success('保存成功')
        //   this.initList()
        // })
      },
      handleDelete(row) {
        this.$confirm({
          title: '提示',
          content: `确认删除用户【${row.user.name} - ${row.user.phone}】在排行榜中的排名吗？`,
          onOk: () => {
            this.$http.delete(`/agent/rank/${row.id}`).then(res => {
              this.$message.success('删除成功')
              this.initList(this.session.start_day)
            })
          }
        })
      }
    }
  }
</script>
<style lang="less" scoped>
  .user {
    display: flex;
    // flex-wrap: wrap;
    align-items: center;
  
    img {
      width: 26px;
      height: 26px;
      margin-right: 6px;
      border-radius: 50%;
      overflow: hidden;
    }
    }
  .thumb {
    width: 30px;
    // height: 60px;
    border-radius: 6px;
  }
  .btn-c {
    display: flex;
    // margin-top: 30px;
    padding: 0rpx 30rpx;

    .add {}

    .save {
      margin-right: 20px;
    }

    .cancel {}
  }

  .tips {
    color: gray;
    font-size: 16px;
    margin-bottom: 6px;
  }

  a-input {
    display: inline;
  }
</style>
